package com.reliable.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reliable.api.dto.user.ReliableUserLite;
import com.reliable.api.param.IdParam;
import com.reliable.api.param.PageParam;
import com.reliable.api.param.invite.InviteSaveParam;
import com.reliable.api.param.invite.InviteUpdateStatus;
import com.reliable.common.core.domain.AjaxResult;
import com.reliable.common.core.page.TableDataInfo;
import com.reliable.framework.utils.JwtUtils;
import com.reliable.service.domain.ReliableInvite;
import com.reliable.service.service.IReliableInviteService;
import com.reliable.service.utils.IdEncryptUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 邀请码Controller
 * 
 * @author ruoyi
 * @date 2025-07-09
 */
@RestController
@RequestMapping("/reliable/invite")
@Api(value = "邀请码控制器", tags = {"邀请码管理"})
public class ReliableInviteController {

    @Autowired
    private IReliableInviteService reliableInviteService;




    /**
     * 创建邀请码
     */
    @ApiOperation("创建邀请码")
    @PostMapping("/createInvite")
    public AjaxResult create(@RequestBody InviteSaveParam  param) {
        return reliableInviteService.create(param);
    }

    @ApiOperation("查询邀请码列表")
    @PostMapping("/inviteList")
    public AjaxResult list(@RequestBody PageParam param) {

        ReliableUserLite currentUser = JwtUtils.getCurrentUser();
        if (!currentUser.getUserType().equals(1L))
        {
            return AjaxResult.warn("无权限");
        }

        IPage<ReliableInvite> page = reliableInviteService.page(new Page<>(param.getPageNum(), param.getPageSize()));
        for (ReliableInvite record : page.getRecords()) {
            record.setBusinessId(IdEncryptUtils.encrypt(record.getId()));
            record.setId(null);
        }
        TableDataInfo data = new TableDataInfo(page.getRecords(), (int) page.getTotal());
        return AjaxResult.success(data);
    }

    @ApiOperation("查询邀请码详情")
    @PostMapping("/InviteInfo")
    public AjaxResult info(@RequestBody IdParam param) {
        param.setId(IdEncryptUtils.decrypt(param.getBusinessId()));

        ReliableUserLite currentUser = JwtUtils.getCurrentUser();

        if (!currentUser.getUserType().equals(1L))
        {
            return AjaxResult.warn("无权限");
        }
        ReliableInvite entity = reliableInviteService.getById(param.getId());
        entity.setBusinessId(IdEncryptUtils.encrypt(entity.getId()));
        entity.setId(null);
        entity.setDelFlag( null);
        return AjaxResult.success(entity);
    }

    @ApiOperation("修改邀请码状态")
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody InviteUpdateStatus param) {

        ReliableUserLite currentUser = JwtUtils.getCurrentUser();
        if (!currentUser.getUserType().equals(1L))
        {
            return AjaxResult.warn("无权限");
        }

        param.setId(IdEncryptUtils.decrypt(param.getBusinessId()));
      return   reliableInviteService.updateStatus(param);
    }

    @ApiOperation("删除邀请码")
    @PostMapping("/deleteInvite")
    public AjaxResult remove(@RequestBody IdParam param) {

        ReliableUserLite currentUser = JwtUtils.getCurrentUser();

        if (!currentUser.getUserType().equals(1L))
        {
            return AjaxResult.warn("无权限");
        }

        param.setId(IdEncryptUtils.decrypt(param.getBusinessId()));
        return reliableInviteService.removeById(param.getId()) ? AjaxResult.success() : AjaxResult.error();
    }









//
//    /**
//     * 查询邀请码列表
//     */
//    @ApiOperation("查询邀请码列表")
//    @PreAuthorize("@ss.hasPermi('system:invite:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(reliableInvite reliableInvite) {
//        startPage();
//        List<reliableInvite> list = reliableInviteService.list(new QueryWrapper<reliableInvite>(reliableInvite));
//        return getDataTable(list);
//    }
//
//    /**
//     * 导出邀请码列表
//     */
//    @ApiOperation("导出邀请码列表")
//    @PreAuthorize("@ss.hasPermi('system:invite:export')")
//    @Log(title = "邀请码", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(reliableInvite reliableInvite) {
//        List<reliableInvite> list = reliableInviteService.list(new QueryWrapper<reliableInvite>(reliableInvite));
//        ExcelUtil<reliableInvite> util = new ExcelUtil<reliableInvite>(reliableInvite.class);
//        return util.exportExcel(list, "邀请码数据");
//    }
//
//    /**
//     * 获取邀请码详细信息
//     */
//    @ApiOperation("获取邀请码详细信息")
//    @PreAuthorize("@ss.hasPermi('system:invite:query')")
//    @GetMapping(value = "/{id}")
//    public AjaxResult getInfo(@PathVariable("id") Long id) {
//        return AjaxResult.success(reliableInviteService.getById(id));
//    }
//
//    /**
//     * 新增邀请码
//     */
//    @ApiOperation("新增邀请码")
//    @PreAuthorize("@ss.hasPermi('system:invite:add')")
//    @Log(title = "邀请码", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody reliableInvite reliableInvite) {
//        return toAjax(reliableInviteService.save(reliableInvite));
//    }
//
//    /**
//     * 修改邀请码
//     */
//    @ApiOperation("修改邀请码")
//    @PreAuthorize("@ss.hasPermi('system:invite:edit')")
//    @Log(title = "邀请码", businessType = BusinessType.UPDATE)
//    @PutMapping
//    public AjaxResult edit(@RequestBody reliableInvite reliableInvite) {
//        return toAjax(reliableInviteService.updateById(reliableInvite));
//    }
//
//    /**
//     * 删除邀请码
//     */
//    @ApiOperation("删除邀请码")
//    @PreAuthorize("@ss.hasPermi('system:invite:remove')")
//    @Log(title = "邀请码", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids) {
//        return toAjax(reliableInviteService.removeByIds(Arrays.asList(ids)));
//    }
}
